/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import common.Item;

public class ItemTableModel extends AbstractTableModel {
    List<Item> list = new ArrayList<>();
    
    private String[] titles;

    public ItemTableModel() {
        this.titles = new String[6];
        titles[0] = "Name";
        titles[1] = "Description";
        titles[2] = "Owner";
        titles[3] = "Current bidder";
        titles[4] = "Current bid";
        titles[5] = "Auction time";
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return titles.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return ((Item)list.get(rowIndex)).getName();
            } else if (columnIndex == 1) {
                return ((Item)list.get(rowIndex)).getDescription();
            } else if (columnIndex == 2) {
                return ((Item)list.get(rowIndex)).getOwner();
            }  else if (columnIndex == 3) {
                return ((Item)list.get(rowIndex)).getCurrentBidder();
            } else if (columnIndex == 4) {
                return ((Item)list.get(rowIndex)).getCurrentBid();
            } else if (columnIndex == 5) {
                return ((Item)list.get(rowIndex)).getAuctionTime();
            }
        return null;
    }
    
    @Override
    public String getColumnName(int i) {
        return titles[i];
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
    
    public void update(List<Item> lista) {
        this.list.clear();
        this.list.addAll(lista);
        fireTableDataChanged();
    }
    
    public void remove(Item item) {
    	if (this.list.indexOf(item)!=-1) {
	    	this.list.remove(this.list.indexOf(item));
	    	fireTableDataChanged();
    	}
    }
    
    public void addOrUpdate(Item item) {
    	if (this.list.indexOf(item)==-1) {
    		this.list.add(item);
    	} else {
    		this.list.set(this.list.indexOf(item), item);
    	}
    	fireTableDataChanged();
    }
    
    public Item getRecordAt(int row) {
        return this.list.get(row);
    }
    
}
